package com.aop.cglib;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * CGLIB代理对象
 * @author My
 *
 */
public class BookFacadeProxy implements MethodInterceptor {

	private Object triget;
	
	
	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		
		 System.out.println("事物开始");  
	     proxy.invokeSuper(obj, args);  
	     System.out.println("事物结束");  
		return null;
	}

	public Object createProxy(Object triget){
		
		this.triget=triget;
		Enhancer enhancer = new Enhancer();
		
		//this.triget.getClass() 把当前目标对象传入
		enhancer.setSuperclass(this.triget.getClass());
		
		//回调方法
		enhancer.setCallback(this);
		
		// 创建代理对象  
		return enhancer.create();
		
	}
	
}
